<template>
  <el-dialog
    :title="!dataForm.psmId ? '新增' : '修改'"
    :close-on-click-modal="false"
    :visible.sync="visible">
    <el-form :model="dataForm" :rules="dataRule" ref="dataForm" @keyup.enter.native="dataFormSubmit()" label-width="80px">
    <el-form-item label="商品名称" prop="psmName">
      <el-input v-model="dataForm.psmName" placeholder="商品名称"></el-input>
    </el-form-item>
    <el-form-item label="商品标题" prop="psmTitle">
      <el-input v-model="dataForm.psmTitle" placeholder="商品标题"></el-input>
    </el-form-item>
    <el-form-item label="商品展示" prop="psmImg">
      <el-input v-model="dataForm.psmImg" placeholder="商品展示"></el-input>
    </el-form-item>
    <el-form-item label="重量" prop="psmWeight">
      <el-input v-model="dataForm.psmWeight" placeholder="重量"></el-input>
    </el-form-item>
    <el-form-item label="品牌" prop="psmBrand">
      <el-input v-model="dataForm.psmBrand" placeholder="品牌"></el-input>
    </el-form-item>
    <el-form-item label="库存" prop="psmStock">
      <el-input v-model="dataForm.psmStock" placeholder="库存"></el-input>
    </el-form-item>
    <el-form-item label="商品价格" prop="psmPrice">
      <el-input v-model="dataForm.psmPrice" placeholder="商品价格"></el-input>
    </el-form-item>
    </el-form>
    <span slot="footer" class="dialog-footer">
      <el-button @click="visible = false">取消</el-button>
      <el-button type="primary" @click="dataFormSubmit()">确定</el-button>
    </span>
  </el-dialog>
</template>

<script>
  export default {
    data () {
      return {
        visible: false,
        dataForm: {
          psmId: 0,
          psmName: '',
          psmTitle: '',
          psmImg: '',
          psmCreateTime: '',
          psmUpdateTime: '',
          psmWeight: '',
          psmBrand: '',
          psmStock: '',
          psmPrice: ''
        },
        dataRule: {
          psmName: [
            { required: true, message: '商品名称不能为空', trigger: 'blur' }
          ],
          psmTitle: [
            { required: true, message: '商品标题不能为空', trigger: 'blur' }
          ],
          psmImg: [
            { required: true, message: '商品展示不能为空', trigger: 'blur' }
          ],
          psmCreateTime: [
            { required: true, message: '创建时间不能为空', trigger: 'blur' }
          ],
          psmUpdateTime: [
            { required: true, message: '修改时间不能为空', trigger: 'blur' }
          ],
          psmWeight: [
            { required: true, message: '重量不能为空', trigger: 'blur' }
          ],
          psmBrand: [
            { required: true, message: '品牌不能为空', trigger: 'blur' }
          ],
          psmStock: [
            { required: true, message: '库存不能为空', trigger: 'blur' }
          ],
          psmPrice: [
            { required: true, message: '商品价格不能为空', trigger: 'blur' }
          ]
        }
      }
    },
    methods: {
      init (id) {
        this.dataForm.psmId = id || 0
        this.visible = true
        this.$nextTick(() => {
          this.$refs['dataForm'].resetFields()
          if (this.dataForm.psmId) {
            this.$http({
              url: this.$http.adornUrl(`/product/goods/info/${this.dataForm.psmId}`),
              method: 'get',
              params: this.$http.adornParams()
            }).then(({data}) => {
              if (data && data.code === 0) {
                this.dataForm.psmName = data.goods.psmName
                this.dataForm.psmTitle = data.goods.psmTitle
                this.dataForm.psmImg = data.goods.psmImg
                this.dataForm.psmCreateTime = data.goods.psmCreateTime
                this.dataForm.psmUpdateTime = data.goods.psmUpdateTime
                this.dataForm.psmWeight = data.goods.psmWeight
                this.dataForm.psmBrand = data.goods.psmBrand
                this.dataForm.psmStock = data.goods.psmStock
                this.dataForm.psmPrice = data.goods.psmPrice
              }
            })
          }
        })
      },
      // 表单提交
      dataFormSubmit () {
        this.$refs['dataForm'].validate((valid) => {
          if (valid) {
            this.$http({
              url: this.$http.adornUrl(`/product/goods/${!this.dataForm.psmId ? 'save' : 'update'}`),
              method: 'post',
              data: this.$http.adornData({
                'psmId': this.dataForm.psmId || undefined,
                'psmName': this.dataForm.psmName,
                'psmTitle': this.dataForm.psmTitle,
                'psmImg': this.dataForm.psmImg,
                'psmCreateTime': this.dataForm.psmCreateTime,
                'psmUpdateTime': this.dataForm.psmUpdateTime,
                'psmWeight': this.dataForm.psmWeight,
                'psmBrand': this.dataForm.psmBrand,
                'psmStock': this.dataForm.psmStock,
                'psmPrice': this.dataForm.psmPrice
              })
            }).then(({data}) => {
              if (data && data.code === 0) {
                this.$message({
                  message: '操作成功',
                  type: 'success',
                  duration: 1500,
                  onClose: () => {
                    this.visible = false
                    this.$emit('refreshDataList')
                  }
                })
              } else {
                this.$message.error(data.msg)
              }
            })
          }
        })
      }
    }
  }
</script>
